
package models;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import play.db.jpa.JPA;

public class Store {

    public static EntityManager em() {
        EntityManager result = JPA.em();
        if (result == null) {
            result = JPA.em("default");
            JPA.bindForCurrentThread(result);
        }

        return result;
    }

    public static class Transaction {

        Transaction() {
        }

        public void save(final Object... entities) {
            if (entities.length == 0) {
                return;
            }

            EntityManager em = null;
            EntityTransaction tx = null;
            try {

                em = JPA.em("default");
                tx = em.getTransaction();
                tx.begin();

                for (Object entity : entities) {
                    em.persist(entity);
                }

                if (tx != null) {
                    if (tx.getRollbackOnly()) {
                        tx.rollback();
                    } else {
                        tx.commit();
                    }
                }

            } catch (Throwable t) {
                if (tx != null) {
                    try {
                        tx.rollback();
                    } catch (Throwable e) {
                    }
                }
                throw t;
            } finally {
                if (em != null) {
                    em.close();
                }
            }
        }

        public void delete(final Object... entities) {
            if (entities.length == 0) {
                return;
            }

            EntityManager em = null;
            EntityTransaction tx = null;
            try {

                em = JPA.em("default");
                tx = em.getTransaction();
                tx.begin();

                for (Object entity : entities) {
                    em.remove(entity);
                }

                if (tx != null) {
                    if (tx.getRollbackOnly()) {
                        tx.rollback();
                    } else {
                        tx.commit();
                    }
                }

            } catch (Throwable t) {
                if (tx != null) {
                    try {
                        tx.rollback();
                    } catch (Throwable e) {
                    }
                }
                throw t;
            } finally {
                if (em != null) {
                    em.close();
                }
            }
        }
    }

    public static void save(final Object entity) {
        JPA.em().persist(entity);
    }

    public static void save(final Object... entities) {
        if (entities.length == 0) {
            return;
        }
        for (Object entity : entities) {
            JPA.em().persist(entity);
            JPA.em().getTransaction();
        }
    }

    public static void delete(final Object entity) {
        if (entity == null) {
            return;
        }
        JPA.em().remove(entity);
    }

    public static void delete(final Object... entities) {
        if (entities.length == 0) {
            return;
        }
        for (Object entity : entities) {
            JPA.em().remove(entity);
        }
    }

    public static void refresh(final Object entity) {
        JPA.em().refresh(entity);
    }

}
